package server.io.code;

import java.lang.reflect.Type;
import java.util.Collection;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class EnDeCode_JSON extends EnDeCode_Base
{
	/**
	 * gson,json工具类
	 */
	public static Gson gson = new Gson();

	/**
	 * string转成list
	 * 
	 * @param argData
	 * @param argType
	 * @return
	 */
	public static Collection<?> transitJsonToList(String argData, Type argType)
	{
		Collection<?> _listObj = null;
		if (argData != null && !argData.equals(""))
		{
			_listObj = gson.fromJson(argData, argType);
		}
		return _listObj;
	}

	/**
	 * json转单个对象
	 * 
	 * @param argData
	 * @param argType
	 * @return
	 */
	public static Object transitJsonToObject(String argData, Type argType)
	{
		Object _result = null;
		if (argData != null && !argData.equals(""))
		{
			_result = gson.fromJson(argData, argType);
		}
		return _result;
	}
	
	/**
	 * 单个对象转json
	 * 
	 * @param argObj
	 * @param argType
	 * @return
	 */
	public static String transitObjectToJson(Object argObj, Type argType)
	{
		String _result = gson.toJson(argObj, argType);
		return _result;
	}

	/**
	 * 单个对象转json
	 * 
	 * @param argObj
	 * @param argType
	 * @return
	 */
	public static String transitObjectToJson(Object argObj)
	{
		String _result = gson.toJson(argObj);
		return _result;
	}
	
	
	public static String objToJson_Condition(Object argObj,
			final String condition)
	{
		String _result = null;
		
		Gson _gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy(){

			@Override
			public boolean shouldSkipClass(Class<?> arg0)
			{
				return false;
			}

			@Override
			public boolean shouldSkipField(FieldAttributes arg0)
			{
				// TODO Auto-generated method stub
				boolean _result = false;
				// TODO Auto-generated method stub
				if(arg0.getName().contains("nat_"))
				{
					_result = true;
				}
				return _result;

			}
			
		}).create();
		_result = _gson.toJson(argObj);
		
		return _result;
	}
	
	

	public static String transitByCondition(Object argObj,
			final String condition)
	{
		Gson _gson = new GsonBuilder().setExclusionStrategies(
				new ExclusionStrategy()
				{

					@Override
					public boolean shouldSkipField(FieldAttributes arg0)
					{
						boolean _result = false;
						// TODO Auto-generated method stub
						// 每一组条件
						String _eachCondition[] = condition.split("[|]");
						String _ownerClassName = arg0.getDeclaringClass()
								.getSimpleName();
						String _fieldName = arg0.getName();

						for (int i = 0; i < _eachCondition.length; i++)
						{
							if (_eachCondition[i] == null
									|| _eachCondition[i].equals(""))
							{

							}
							else
							{
								String _each[] = _eachCondition[i].split("-");
								String _fieldOrClass = _each[0];
								String _class = _each[1];
								String _fields = _each[2];
								String _exOrin = _each[3];

								if (_class.equals(_ownerClassName)
										&& "field".equals(_fieldOrClass))
								{
									_result = exOrNot(_class, _fields,
											_fieldName, _ownerClassName,
											_exOrin);
								}
							}
						}
						return _result;
					}

					@Override
					public boolean shouldSkipClass(Class<?> arg0)
					{
						// TODO Auto-generated method stub
						boolean _result = false;

						return _result;
					}

					public boolean exOrNot(String declareclass, String fields,
							String fieldName, String className, String exOrIn)
					{
						boolean _result = false;
						if (condition == null || condition.equals(""))
						{
							_result = false;
						}
						else
						{
							if (fields.contains(fieldName))
							{
								_result = "ex".equals(exOrIn) ? true : false;
							}
							else
							{
								_result = "ex".equals(exOrIn) ? false : true;
							}
						}
						return _result;
					}

				}).create();

		String _result = _gson.toJson(argObj);

		return _result;
	}

}
